package com.journaldev.spring;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.journaldev.spring.model.City;
import com.journaldev.spring.model.District;
import com.journaldev.spring.model.Ward;
import com.journaldev.spring.service.CityService;
import com.journaldev.spring.service.DistrictService;
import com.journaldev.spring.service.WardService;
import com.journaldev.spring.util.SimpleCityEditor;
import com.journaldev.spring.util.SimpleDistrictEditor;

@Controller
public class WardController {
	
	private WardService wardService;
	
	private DistrictService districtService;
	
	private CityService cityService;
	
	@InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        //binder.registerCustomEditor(City.class, new SimpleCityEditor(cityService));
        binder.registerCustomEditor(District.class, new SimpleDistrictEditor(districtService));
    }
	
	@Autowired(required=true)
	@Qualifier(value="cityService")
	public void setCityService(CityService ps){
		this.cityService = ps;
	}
	
	@Autowired(required=true)
	@Qualifier(value="districtService")
	public void setDistrictService(DistrictService ps){
		this.districtService = ps;
	}
	
	@Autowired(required=true)
	@Qualifier(value="wardService")
	public void setWardService(WardService wardService) {
		this.wardService = wardService;
	}
	
	@RequestMapping(value = {"/wards"}, method = RequestMethod.GET)
	public String listWards(Model model) {
		model.addAttribute("ward", new Ward());
		model.addAttribute("listWard", this.wardService.listWard());
		model.addAttribute("listDistrict", this.districtService.listDistrict());
		return "ward";
	}
	
	@RequestMapping(value = {"/ward/add"}, method = RequestMethod.POST)
	public String addWard(@ModelAttribute("ward") Ward p,  BindingResult result, Model model, RedirectAttributes redirectAttrs) {
		if(result.hasErrors()) {
			model.addAttribute("message", "add errors");
			return "redirect:/wards";
		}
		if(p.getId() != 0) {
			this.wardService.updateWard(p);
			redirectAttrs.addFlashAttribute("message", "update success id =" + p.getId());
		} else {
			this.wardService.addWard(p);
			redirectAttrs.addFlashAttribute("message", "add success");
		}
		model.asMap().clear();
		return "redirect:/wards";
	}
	
	@RequestMapping(value = {"/ward/remove/{id}"})
	public String removeWard(@PathVariable("id") int id) {
		Ward temp = this.wardService.getWardById(id);
		District districtObj= this.districtService.getDistrictById(temp.getBelongToDistrict().getId());
		Iterator<Ward> ite = districtObj.getWard().iterator();
		Ward element = null;
		while(ite.hasNext()) {
			 element = ite.next();
	         if(element.getId() == id) {
	        	 break;
	         }
        }
		districtObj.getWard().remove(element);
		//this.cityService.getCityById(temp.getBelongToCity().getId()).setDisTrict(listDistrict);
		this.districtService.updateDistrict(districtObj);
		//this.districtService.updateDistrict(temp);
		//this.districtService.removeDistrict(id);
		return "redirect:/wards";
	}
	
	@RequestMapping("/ward/edit/{id}")
    public String editWard(@PathVariable("id") int id, Model model){
        model.addAttribute("ward", this.wardService.getWardById(id));
        model.addAttribute("listDistrict", this.districtService.listDistrict());
        return "ward";
    }

}
